﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Entity;
using IOA.MES.PdaApi.Models.Request.Load;
using IOA.MES.PdaApi.Models.Response.Load;
using System.Linq;
using System.Web.Http;

namespace IOA.MES.PdaApi.Controllers
{
    /// <summary>
    /// 装车管理
    /// </summary>
    public class LoadController : ApiController
    {
        /// <summary>
        /// 装车
        /// </summary>
        [HttpPost]
        public ResultJson<LoadPackResponse> LoadPack([FromBody] LoadPackRequest request)
        {
            new LoadMgr().LoadPack(new DataAccess.VModel.VLoadPack
            {
                PackNo = request.PackNo,
                ModifyBy = BaseEnv.User.PkId,
                LoadId = request.LoadId
            });

            return new ResultJson<LoadPackResponse>();
        }

        /// <summary>
        /// 完成装车
        /// </summary>
        [HttpPost]
        public ResultJson<FinishLoadResponse> FinishLoad([FromBody] FinishLoadRequest request)
        {
            new LoadMgr().FinishLoad(new DataAccess.VModel.VLoadInspect
            {
                ModifyBy = BaseEnv.User.PkId,
                Driver = request.Driver,
                Phone = request.Phone,
                NumberPlate = request.NumberPlate,
                ActualCabinetNo = request.ActualCabinetNo,
                LockNo = request.LockNo,
                PkId = request.LoadId
            });
            return new ResultJson<FinishLoadResponse>();
        }

        /// <summary>
        /// 查询装车记录
        /// </summary>
        [HttpPost]
        public ResultJson<QueryLoadsResponse> QueryLoads([FromBody] QueryLoadsRequest request)
        {
            var total = 0;
            var loads = new LoadMgr().QueryLoads(request.PageIndex, request.PageSize, out total, request.Finished ? DataAccess.Enum.LoadStatus.Loaded : DataAccess.Enum.LoadStatus.Loading, request.Finished ? BaseEnv.User.PkId : 0);
            return new ResultJson<QueryLoadsResponse>(new QueryLoadsResponse
            {
                Loads = loads.Select(load => new Load
                {
                    LoadId = load.PkId,
                    CabinetNo = load.CabinetNo,
                    FinishTime = load.FinishTime?.ToString("yyyy-MM-dd HH:mm") ?? string.Empty,
                    LoadBy = load.CNLoadBy,
                    LoadTime = load.CreateTime.ToString("yyyy-MM-dd HH:mm"),
                    Quantity = load.Quantity,
                    LoadedQuantity = load.LoadedQuantity
                }).ToList()
            }, total);
        }

        /// <summary>
        /// 获取装车详情
        /// </summary>
        [HttpPost]
        public ResultJson<GetLoadResponse> GetLoad([FromBody] GetLoadRequest request)
        {
            var load = new LoadMgr().GetLoad(request.LoadId);
            return new ResultJson<GetLoadResponse>(new GetLoadResponse
            {
                Load = load == null ? null : new Load
                {
                    LoadId = load.PkId,
                    CabinetNo = load.CabinetNo,
                    FinishTime = load.FinishTime?.ToString("yyyy-MM-dd HH:mm") ?? string.Empty,
                    LoadBy = load.CNLoadBy,
                    LoadTime = load.CreateTime.ToString("yyyy-MM-dd HH:mm"),
                    Quantity = load.Quantity,
                    LoadStatus = load.LoadStatus,
                    LoadedQuantity = load.LoadedQuantity,
                    ActualCabinetNo = load.ActualCabinetNo,
                    Driver = load.Driver,
                    LockNo = load.LockNo,
                    NumberPlate = load.NumberPlate,
                    Phone = load.Phone,
                    Packs = load.Packs.Select(pack => new Models.Response.Load.Pack
                    {
                        PackBatchNo = pack.PackBatchNo,
                        PackNo = pack.PackNo,
                        Loaded = pack.Loaded
                    }).ToList()
                }
            });
        }

        /// <summary>
        /// 移除包装
        /// </summary>
        [HttpPost]
        public ResultJson<UnloadPackResponse> UnloadPack([FromBody] UnloadPackRequest request)
        {
            new LoadMgr().UnloadPack(new LoadPack { PkId = request.LoadId, PackNo = request.PackNo, ModifyBy = BaseEnv.User.PkId });
            return new ResultJson<UnloadPackResponse>();
        }
    }
}
